<template>
  <div class="user">
    <div class="header">
      <div class="total">用户:{{total}}</div>
      <el-button type="primary" @click="openUserEditor">新建用户</el-button>
    </div>
    <el-table :data="tableData" height="calc(100% - 110px)" style="width: 100%">
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="real_name" label="昵称"></el-table-column>
      <el-table-column prop="role" label="邮箱">
        <template slot-scope="scope">{{ scope.row.role === 1 ? '管理员' : '普通用户'}}</template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱">
        <template slot-scope="scope">{{ scope.row.email ? scope.row.email : '-'}}</template>
      </el-table-column>
      <el-table-column prop="mobile" label="电话">
        <template slot-scope="scope">{{ scope.row.mobile ? scope.row.mobile : '-'}}</template>
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间">
        <template
          slot-scope="scope"
        >{{ scope.row.create_time ? dayjs(Number(scope.row.create_time)).format('YYYY-MM-DD HH:mm:ss') : '-'}}</template>
      </el-table-column>
      <el-table-column prop="update_time" label="更新时间">
        <template
          slot-scope="scope"
        >{{ scope.row.update_time ? dayjs(Number(scope.row.update_time)).format('YYYY-MM-DD HH:mm:ss') : '-'}}</template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="openDelete(scope.row)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :page-size="pageSize"
      :current-page="page"
      @current-change="pageChange"
      layout="prev, pager, next"
      :total="total"
    ></el-pagination>

    <UserEditor :visible="userEditorVisible" @close="close"/>
  </div>
</template>

<script>
import dayjs from "dayjs";
import { createNamespacedHelpers } from "vuex";
const { mapActions } = createNamespacedHelpers("admin");
import UserEditor from "../UserEditor";
export default {
  name: "UserList",
  components: {
    UserEditor,
  },
  data() {
    return {
      pageSize: 10,
      page: 1,
      user_id: 1,
      tableData: [],
      total: 0,

      userEditorVisible: false,
    };
  },
  mounted() {
    this.fetchUserList();
  },
  methods: {
    dayjs,
    ...mapActions(["getUserlist", "deleteUser"]),
    openDelete(row) {
      this.$confirm("确认删除？")
        .then(() => {
          this.user_id = row.user_id;
          this.deleteClick();
        })
        .catch(() => {});
    },
    deleteClick() {
      this.deleteUser(this.user_id).then(() => {
        this.fetchUserList();
      });
    },
    fetchUserList() {
      const params = {
        page: this.page,
        pageSize: this.pageSize
      };
      this.getUserlist(params).then(res => {
        this.tableData = res.list;
        this.total = res.total;
      });
    },
    pageChange(res) {
      this.page = res;
    },


    // 新建 & 编辑用户
    openUserEditor() {
      this.userEditorVisible = true;
    },
    close() {
      this.userEditorVisible = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.user {
  height: 100%;
  .header {
    display: flex;
    align-items: center;
    height: 64px;
    overflow-x: auto;
    justify-content: space-between;
  }
  .el-table {
    //   height: calc(100% - 110px);
    //   overflow: hidden;
    // /deep/  .el-table__body-wrapper {
    //     height: calc(100% - 48px);
    //     overflow: auto;
    //   }
  }
}
</style>